package com.hxm.meizi.provider;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxm.meizi.R;
import com.hxm.meizi.entity.ImageUrl;
import com.hxm.meizi.entity.ZiPaiImage;
import com.hxm.meizi.ui.activity.ImagePreviewActivity;
import com.hxm.meizi.util.ImageLoader;

import java.util.ArrayList;

import me.drakeet.multitype.ItemViewProvider;

/**
 * Created by hxm on 2017/2/19.
 */

public class ZiPaiProvider extends ItemViewProvider<ZiPaiImage, ZiPaiProvider.ZiPaiHolder> {

    @NonNull
    @Override
    protected ZiPaiHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_zi_pai_provider, parent, false);
        return new ZiPaiHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ZiPaiHolder holder, @NonNull final ZiPaiImage imageInfo) {
        final Context context = holder.itemView.getContext();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageUrl imageUrl = new ImageUrl();
                imageUrl.setUrl(imageInfo.getUrl());
                imageUrl.setId(1);
                ArrayList<ImageUrl> imageUrls = new ArrayList<>();
                imageUrls.add(imageUrl);
                Intent intent = new Intent(context, ImagePreviewActivity.class);
                intent.putParcelableArrayListExtra("item_data", imageUrls);
                context.startActivity(intent);
            }
        });
        holder.tvTime.setText(imageInfo.getUploadTime());
        ImageLoader.display(context, imageInfo.getUrl(), holder.imageView, holder.loading, holder.loadingError);
    }

    static class ZiPaiHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tvTime;
        private LinearLayout loading, loadingError;

        public ZiPaiHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            loading = (LinearLayout) itemView.findViewById(R.id.loading);
            loadingError = (LinearLayout) itemView.findViewById(R.id.loading_error);
        }
    }
}
